﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using EyFramework.Common;
using YbService.Core.Service;
using YbService.Entity.Dto;
using YbService.Entity.Hzyb;

namespace HzybService
{
    /// <summary>
    /// 封装杭州医保的动态库
    /// </summary>
    public static class BaseHzyb
    {
        [DllImport("HZSiInterface.dll", SetLastError = true, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        private static extern int INIT([MarshalAs(UnmanagedType.LPArray)] byte[] pErrMsg);

        [DllImport("HZSiInterface.dll", SetLastError = true, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        private static extern int BUSINESS_HANDLE([MarshalAs(UnmanagedType.LPArray)] byte[] Data, [MarshalAs(UnmanagedType.LPArray)] byte[] retMsg);

        #region 参数

        /// <summary>
        /// 错误信息存放
        /// </summary>
        private static string ErrText;

        /// <summary>
        /// 错误信息存放
        /// </summary>
        private static string _ErrText
        {
            set
            {
                ErrText = value;
            }
            get
            {
                return ErrText;
            }
        }

        /// <summary>
        /// 错误信息存放
        /// </summary>
        public static string ErrorText
        { get { return _ErrText; } }

        /// <summary>
        /// 是否已经初始化
        /// </summary>
        private static bool IsInit = false;

        #endregion 参数

        #region 封装dll里的函数

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="AutoInit">是否自动初始化,当为false时强制初始化</param>
        /// <returns>0-成功 ，-1-失败</returns>
        public static int HZYTH_INIT(bool AutoInit)
        {
            if (AutoInit == true && IsInit == true) return 0;//如果已经初始化了就返回成功
            byte[] pErrMsg = new byte[20000];
            HzybLogInData logInData = new HzybLogInData()
            {
                MzzyLsh = "",
                RuCan = "",
                JiaoYiLx = "",
                JiaoYiLxmc = "初始化",
                YongHuId = "DBA",
                YeWuZqh = "",
                YiYuanJylsh = "",
                KaLeiXing = "",
                LianJiBz = "1"
            };
            long LogId = PublicService.SaveHzybInLog(logInData);
            int iResult = INIT(pErrMsg);
            if (iResult < 0)
            {
                IsInit = false;
                _ErrText = System.Text.Encoding.Default.GetString(pErrMsg).Trim('\0');
                BizException ex = new BizException("杭州医保初始化失败:" + _ErrText);
                HzybLogOutData logOutData = new HzybLogOutData()
                {
                    LOGID = LogId,
                    CHUCAN = _ErrText,
                    JIAOYIZT = iResult.ToString()
                };
                PublicService.SaveHzybOutLog(logOutData);
                throw ex;
            }
            IsInit = true;
            HzybLogOutData logOutDataSucess = new HzybLogOutData()
            {
                LOGID = LogId,
                CHUCAN = Encoding.Default.GetString(pErrMsg).Trim('\0'),
                JIAOYIZT = "0"
            };
            PublicService.SaveHzybOutLog(logOutDataSucess);
            return iResult;
        }

        /// <summary>
        /// 交易函数
        /// </summary>
        /// <param name="param"></param>
        /// <param name="sRetMsg"></param>
        /// <returns></returns>
        public static HzybReturn HzybBusinessHandle(HzybBusinessParam param)
        {
            HZYTH_INIT(true);//初始化
            byte[] InptData = new byte[50000];
            byte[] OutData = new byte[50000];
            InptData = System.Text.Encoding.Default.GetBytes(param.ShuRuCanshu);
            HzybLogInData logInData = new HzybLogInData()
            {
                MzzyLsh = param.MzzyLsh,
                RuCan = param.ShuRuCanshu,
                JiaoYiLx = param.JiaoYiLx,
                JiaoYiLxmc = param.JiaoYiLxmc,
                YongHuId = "DBA",
                YeWuZqh = param.YeWuZqh,
                YiYuanJylsh = param.YewuJylsh,
                KaLeiXing = param.KaLeiXing,
                LianJiBz = "1"
            };
            long LogId = PublicService.SaveHzybInLog(logInData);
            int iResult = BUSINESS_HANDLE(InptData, OutData);
            string sRetMsg = System.Text.Encoding.Default.GetString(OutData).Trim('\0');
            HzybReturn hzybReturn = new HzybReturn(iResult, sRetMsg);
            HzybLogOutData logOutData = new HzybLogOutData()
            {
                LOGID = LogId,
                CHUCAN = sRetMsg,
                JIAOYIZT = iResult.ToString()
            };
            PublicService.SaveHzybOutLog(logOutData);
            if (iResult < 0)
            {
                BizException ex = new BizException("杭州医保交易失败:" + sRetMsg);
                throw ex;
            }
            return hzybReturn;
        }

        #endregion 封装dll里的函数
    }
}